#!/bin/sh

binary="/sbin/dhcpcd"
pidfile="/var/run/dhcpcd-${interface}.pid"

netif_create() {
    brctl addbr $interface
}

netif_destroy() {
    brctl delbr $interface
}

netif_post_up() {
    if [ -f "${pidfile}" ]; then
        echo "There's a PID file for interface $interface. Aborting."
        exit 2
    fi

    binary $interface
}

netif_pre_up() {
    if [ ! -f "${binary}" ]; then
        echo "dhcpcd binary not found. Did you emerge net-misc/dhcpcd?"
        exit 1
    fi

    local slave
    local bcast
    if [ -n "$slaves" ]; then
        for slave in $(get_interface_names $slaves); do
            ifconfig ${slave} 0.0.0.0
            brctl addif $interface ${slave}
        done
    fi
    if [ -n "$(if_ipv4 $ipnm)" ]
    then
        bcast="broadcast $(get_option "$ipnm" broadcast +)"
    else
        bcast=""
    fi
    brctl setfd $interface 0
    for ipnm in $ipaddr $ipaddrs; do
        ip addr add $(get_ipnm_part "$ipnm") $bcast \
 dev $interface || die "Couldn't add $ipnm to $interface"
    done

    if [ -n "$stp" ]; then
        brctl stp $interface $stp
    fi

    if [ -n "$forwarding" ]; then
        /sbin/sysctl net.ipv4.conf.${interface}.forwarding=${forwarding} > /dev/null 2>&1
    fi
}


netif_pre_down() {
    if [ ! -f "${pidfile}" ]; then
        echo "There's no PID file for interface ${interface}. Aborting."
        exit 3
    fi

    start-stop-daemon --stop --exec "${binary}" --pidfile "${pidfile}"
}
